import { createApp } from 'vue';
import { createPinia } from 'pinia';
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';
import mitt from 'mitt';
import ElementPlus from 'element-plus';
import zhCn from 'element-plus/dist/locale/zh-cn.min.mjs';
import 'element-plus/dist/index.css';

import App from './App.vue';
import router from './router';

import './assets/main.css';
import './assets/css/init.css';

// 自动注册指令的逻辑
const modules: Record<string, { default: any }> = import.meta.glob('./directive/**/*.ts', {
  eager: true
});

let mapDirective = new Map();

Object.keys(modules).forEach((key) => {
  if (modules[key] && modules[key].default) {
    const newKey = key.replace(/^\.\/|\.ts|\.js/g, '');
    mapDirective.set(newKey, modules[key].default);
  }
});

const app = createApp(App);
const bus = mitt();
app.provide('$bus', bus);
app.config.globalProperties.$bus = bus;

const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);

app.use(pinia);
app.use(router);
app.use(ElementPlus, {
  locale: zhCn,
});

// 注册指令
mapDirective.forEach((value, key) => {
  app.directive(key, value);
});

app.mount('#app');
